#include <iostream>
#include <vector>
#include <stack>
using namespace std;
struct TreeNode {
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode() : val(0), left(nullptr), right(nullptr) {}
	TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
	TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
/**
 * 递归的方式
 */
class Solution1{
private:
	void traversal(TreeNode* node,vector<int>& result){
		if(node == nullptr)
			return;
		result.push_back(node->val);
		traversal(node->left,result);
		traversal(node->right,result);
	}
public:
	vector<int> preorderTraversal(TreeNode* root) {
		vector<int> result;
		traversal(root,result);
		return result;
	}
};

/**
 * 非递归的方式
 */
class Solution2{
public:
	vector<int> preorderTraversal(TreeNode* root) {
		vector<int> result;
		stack<TreeNode*> st;
		if(root != nullptr) st.push(root);
		while(!st.empty()){
			TreeNode* node = st.top();st.pop();
			result.push_back(node->val);
			if(node->right) st.push(node->right);
			if(node->left) st.push(node->left);
		}
		return result;
	}
};

/**
 * 统一迭代的方式
 */
class Solution3{
public:
	vector<int> preorderTraversal(TreeNode* root){
		stack<TreeNode*> st;
		vector<int> result;
		if(root != nullptr) st.push(root);
		while(!st.empty()){
			TreeNode* node = st.top();st.pop();
			if(node != nullptr){
				if(node->right) st.push(node->right);
				if(node->left) st.push(node->left);
				st.push(node);
				st.push(nullptr);
			}else{
				node = st.top();st.pop();
				result.push_back(node->val);
			}
		}
		return result;
	}
};

int main(){
	
	return 0;
}
